<?php

namespace App\Http\Controllers;


use Carbon\Carbon;
use GuzzleHttp\Client;
use Illuminate\Support\Facades\Http;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;

class AsyncPostController extends Controller
{
    /**
     * 发送异步 POST 请求
     */
    public function sendAsyncPost(Request $request,Client $client)
    {
        // 示例请求数据
        $data = [
            'key' => 'agagagag',
            'data' => 'adsfasdf',
        ];
        // 异步发送 POST 请求
        Http::async()
            ->post('https://testadminapi.hangyunejia.com/adminapi/refund/restore/list?log_id=28&business_type=22')
            ->then(function ($response) {
                // 请求成功后的回调
                dd($response->getBody()->getContents());
            })
            ->wait(false); // 非阻塞等待
//        sleep(10);
        dd(1);
        return response()->json(['message' => 'POST request sent asynchronously.']);
    }

    /**
     * 处理异步 POST 请求的回调
     */
    public function handleCallback(Request $request)
    {

        $year = 2024;
        $host = "https://jiejiari.market.alicloudapi.com";
        $path = "/your_inversday";
        $method = "GET";
        $appcode = "bba55b30af8c4afbb6c9819b73233ac6";
        $headers = array();
        array_push($headers, "Authorization:APPCODE " . $appcode);
        $querys = "year={$year}";
        $bodys = "";
        $url = $host . $path . "?" . $querys;


        $curl = curl_init();
        curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($curl, CURLOPT_FAILONERROR, false);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_HEADER, false);
//        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);

        if (1 == strpos("$".$host, "https://"))
        {
            curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
        }
        $res = curl_exec($curl);
        $res = json_decode($res,true);
        if( !empty(data_get($res,'showapi_res_body.inverse_days'))){
            $inverseDays = data_get($res,'showapi_res_body.inverse_days',[]);
            try {
                $return = [];

                $fu = function($begin,$end){
                    $res = [];
                    if($begin<=$end){
                        while ($begin<= $end){
                            $res[] = $begin;
                            $begin = Carbon::parse($begin)->addDay()->format('Ymd');
                        }
                    }
                    return $res;
                };
                foreach ($inverseDays as $value){
                    $begin          = $value['begin'];
                    $end            = $value['end'];
                    $tmpInverseDays = $fu($begin, $end);
                    $return[]       = $tmpInverseDays;
                }
//                $return = array_flatten($return);
                dd($return);
            }catch (\Exception $ex){
                throw new \Exception('获取调休日接口出错 ，请联系开发人员吧');
            }
        }else{

            throw new \Exception('获取调休日接口出错 ，请联系开发人员');
        }
    }


}
